package cms.simmytech.web.cms;

import cms.simmytech.model.SysUser;
import cms.simmytech.model.TbBase;
import cms.simmytech.model.TbCategory;
import cms.simmytech.model.TbExaminer;
import cms.simmytech.service.CategoryService;
import cms.simmytech.web.base.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/category")
public class CategoryController extends BaseController {
    @Autowired
    CategoryService categoryService;
    @RequestMapping(value = "/list")
    public String userList(@RequestParam(value = "currentPage", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, Model model,
                           String name,Byte cateType,Byte cateState) {
        cateType=  cateType==null?-99:cateType;
        cateState=  cateState==null?-99:cateState;
        PageInfo<TbCategory> pageInfo = categoryService.getPageList(pageNum, pageSize,name,cateType,cateState);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("name", name);
        model.addAttribute("cateType", cateType);
        model.addAttribute("cateState", cateState);
        return super.toPage("category/list", model);
    }
    @RequestMapping(value = "/add")
    public String add(Integer id, Integer pId, String dictType, Model model) {
        TbCategory bean = null;
        if (id == null) {
            bean = new TbCategory();
        } else {
            bean = categoryService.getBean(id);

        }
        model.addAttribute("bean", bean);

        return super.toPage("category/edit", model);
    }

    @PostMapping(value = "/save")
    public String saveUser(TbCategory bean) {
        categoryService.save(bean);
        return "redirect:list";
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public Map<String, Object> delete(int id) {
        Map<String, Object> map = new HashMap<String, Object>();
        categoryService.delete(id);
        map.put("res", true);
        return map;
    }

}
